package com.tarena.lbs.content.service.impl;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.ArticleRepository;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleContentParam;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleActionPageResultVO;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    @Override
    public PageResult<ArticleVO> pageList(ArticleQuery articleQuery) {
        log.debug("[服务层]处理分页查询文章,入参{}", articleQuery);
        //调用持久化层 并使用PageHelper
        return articleRepository.pageList(articleQuery);
    }

    @Override
    public int addArticle(ArticleContentParam param) throws BusinessException {
        log.debug("[服务层]处理文章新增,入参{}", param);
        // 处理入参 - 初始化状态统计字段
        if (param.getArticleStatus() == null) {
            param.setArticleStatus(1); // 默认设置为待发布状态
        }

        if (param.getAccessCount() == null) {
            param.setAccessCount(0); // 默认访问次数为0
        }

        if (param.getLikeCount() == null) {
            param.setLikeCount(0); // 默认点赞数为0
        }

        if (param.getFavoriteCount() == null) {
            param.setFavoriteCount(0); // 默认收藏数为0
        }

        if (param.getSource() == null) {
            param.setSource(1); // 默认为前端添加
        }

        // 调用公用方法处理新增逻辑
        return addAndUpdateArticle(param);
    }


    @Override
    public int updateArticle(ArticleContentParam param) throws BusinessException {
        log.debug("[服务层]处理文章更新,入参{}", param);
        if (param.getId() == null) {
            throw new BusinessException("400", "文章ID不能为空");
        }
        // 调用公用方法处理更新逻辑
        return addAndUpdateArticle(param);
    }

    @Override
    public ArticleVO articleDetail(String id) {
        log.debug("[服务层]处理文章详情,入参{}", id);
        return articleRepository.selectDetailById(id);
    }

    @Override
    public int deleteArticle(String id) {
        log.debug("[服务层]处理文章删除,入参{}", id);
        return articleRepository.deleteById(id);
    }

    @Override
    public Set<String> articleLabels(ArticleQuery articleQuery) {
        log.debug("[服务层]处理文章标签集合,入参{}", articleQuery);
        return articleRepository.articleLabels(articleQuery);
    }

    @Override
    public int articleBehavior(ArticleActionParam param) {
        log.debug("[服务层]处理用户行为记录,入参{}", param);
        return articleRepository.articleBehavior(param);
    }

    @Override
    public ArticleActionPageResultVO getBehaviorLists(ArticleQuery query) {
        log.debug("[服务层]处理用户行为文章列表查询,入参{}", query);
        return articleRepository.getBehaviorLists(query);
    }

    private int addAndUpdateArticle(ArticleContentParam param) throws BusinessException {
        log.debug("[服务层]处理文章保存/更新公共逻辑,入参{}", param);
        return articleRepository.saveOrUpdate(param);
    }
}
